package com.yricky.memoria.data

import android.database.sqlite.SQLiteDatabase
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import com.yricky.memoria.utils.PdfDoc
import java.io.File
import java.io.InputStream

/**
 * @author Yricky
 * @date 2021/11/25
 */

fun SQLiteDatabase.transaction(transaction:SQLiteDatabase.()->Unit){
    beginTransaction()
    transaction.invoke(this)
    setTransactionSuccessful()
    endTransaction()
}


fun Section.getContentPath(assetName:String):String{
    return File(assetsDir,assetName).path
}
fun SubjectModel.pdfDir(name:String = ""):File = File(assetsDir,"pdf/$name").apply {
    if(!exists()){
        mkdirs()
    }
}

fun SubjectModel.pdfList():List<Pair<String,PdfDoc>>{
    val list = ArrayList<Pair<String,PdfDoc>>()
    pdfDir().listFiles()?.forEach {
        PdfDoc.from(File(it, "doc.pdf"))?.let{ doc->
            list.add(Pair(it.name, doc))
        }
    }
    return list
}

fun SubjectModel.openPdf(name:String):PdfDoc?{
    return PdfDoc.from(File(pdfDir(name),"doc.pdf"))
}

fun SubjectModel.importPdf(name:String,inS:InputStream){
    File(pdfDir(name),"doc.pdf").writeBytes(inS.readBytes())
}
